#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <string>
#include <pthread.h>
#include <sys/types.h>
#include <vector>
#include <cstdlib>

using namespace std;

//2、用类的方式写

class Task
{
public:
    Task()
    {
    }
    void SetData(int x, int y)
    {
        _datax = x;
        _datay = y;
    }
    int excute()
    {
        return _datax + _datay;
    }
    ~Task()
    {
        
    }
private:
    int _datax;
    int _datay;
};

class DataThread : public Task
{
public:
    DataThread(int x, int y, const string& threadname):_threadname(threadname)
    {
        _t.SetData(x, y);
    }
    string getName()
    {
        return _threadname;
    }
    int run()
    {
        return _t.excute();
    }
    
private:
    string _threadname;
    Task _t;
};

class Result
{
public:
    Result()
    {}
    ~Result()
    {}
    void setResult(int result, const string& threadname)
    {
        _result = result;
        _threadname = threadname;
    }
    void Print()
    {
        cout<< _threadname <<" : " << _result << endl;
    }
private:
    int _result;
    string _threadname;
};

//1、创建多线程

const int threadnum = 5;

void* threadTask(void *args)
{
    DataThread* td = static_cast<DataThread*>(args);
    string name = td->getName();
    int result = td->run(); 
    //cout<<name<<" result is: "<<result<<endl;
    Result* res = new Result();
    res->setResult(result, name);
    
    delete td;
    return res;
    //sleep(2);
    // char* threadname = static_cast<char*>(args);
    // while(true)
    // {
    //     cout<<"the thread is running, threadname is: " << threadname << endl;
    //     sleep(2);
    // }
    // delete[] threadname;
    //return nullptr;
}
int main()
{
    vector<pthread_t> threads;
    for(int i=0; i<threadnum; i++)
    {
        //char *threadname = new char[64];//每个线程一个空间——版本1
        char threadname[64];
        snprintf(threadname, 64, "thread-%d", i+1);//线程的名字

        DataThread* td = new DataThread(10, 30, threadname);//构造函数

        //创建线程
        pthread_t tid;
        //pthread_create(&tid, nullptr, threadTask, threadname);//线程传参
        pthread_create(&tid, nullptr, threadTask, td);///////
        threads.push_back(tid);
    }
    //线程等待
    vector<Result*> result_set;
    void* res = nullptr;
    for(auto& e:threads)
    {
        pthread_join(e, &res);
        result_set.push_back((Result*)res);
    }

    for(auto& e:result_set)
    {
        e->Print();
        delete e;
    }
    return 0;
}




// string ToHex(pthread_t tid)
// {
//     char id[64];
//     snprintf(id, sizeof(id), "0x%lx", tid);
//     return id;
// }

// void *newthread(void *arg)
// {
//     int cnt = 5;
//     while(cnt--)
//     {
//         cout<<"new thread is running, pid: " << getpid() << "tid: " << ToHex(pthread_self()) << endl;
//         sleep(1);
//     }
//     return (void*)123;
// }
// int main()
// {
//     pthread_t tid;
//     //创建线程
//     pthread_create(&tid, nullptr, newthread, nullptr);
//     void *ret = nullptr;

//     int n = pthread_join(tid, &ret);
//     cout<<"主线程退出..., n = "<< n << " ret: " << (long long)ret << endl;
//     sleep(4);

//     // int cnt = 8;
//     // while(cnt--)
//     // {
//     //     cout<<"main thread is running, tid: " << ToHex(tid) << endl;
//     //     cout<<"tid: " << ToHex(pthread_self()) <<endl;
//     //     sleep(1);
//     // }
//     return 0;
// }